package net.dlz.example.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.dlz.example.domain.SearchUserResponse;
import net.dlz.example.domain.User;
import net.dlz.example.domain.UserExample;
import net.dlz.example.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;

//    @Override
//    public List<User> listUser() {
//        return userMapper.selectAll();
//    }


    @Override
    public int exit(String email) {
        return 0;
    }

    @Override
    public List<User> listUser() {
        return null;
    }

    @Override
    public void insertUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public List<User> findByName(String name) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(name);
//        userMapper.updateByExample(user,userExample)
        return userMapper.selectByExample(userExample);
    }

    @Override
    public int saveUser(User user) {
        if (StringUtils.isNotBlank(user.getUserId())){
            return userMapper.updateByPrimaryKey(user);
        }else{
            return userMapper.insert(user);
        }

    }

    @Override
    public User findUserByName(String userName) {
        return userMapper.selectByName(userName);
    }

    @Override
    public int deleteUser(String userid) {
        return userMapper.deleteByPrimaryKey(userid);
    }

    @Override
    public SearchUserResponse findUserLinkName(Integer pageNum, String keywords, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        UserExample example = new UserExample();
        example.createCriteria().andUserNameLike("%"+ keywords + "%");
        List<User> users = userMapper.selectByExample(example);
        return new SearchUserResponse(((Page)users).getTotal(),users);
    }


    @Override
    public SearchUserResponse findAllUser(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectByExample(new UserExample());
        return new SearchUserResponse(((Page)users).getTotal(),users);
    }

    @Override
    public User findUserByPrimaryKey(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int exist(String email) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andEmailEqualTo(email);
        List<User> userList = userMapper.selectByExample(userExample);
        if(userList.size()==0){
            return 0;
        }
        return 1;
    }

    @Override
    public int updateByEmail(User user, String email) {
        UserExample example = new UserExample();
        example.or().andEmailEqualTo(email);
        int i = userMapper.updateByExampleSelective(user,example);
        System.out.println(i);
        return i;
    }
}
